package net.portly.cst;

import java.io.Serializable;
import java.util.Map;

import net.portly.cst.flex.FlexItemType;

/**
 * An {@link AssetTypeDescriptor} describes a Flex Family member.
 * 
 * Flex Family members (denoted by their 'typical' table suffixes) contain
 * information about;- Attributes (_A) Parent Definitions (_PD) Flex Definitions
 * (_CD) Parents (_P) Flex assets (_C) Filters (_F)
 * 
 * Each of these members has a Manager class which allows us to identify it. Its
 * meta data file is always located in the ASSET_TYPE directory of a CSDT dump
 * 
 * @author cs76
 * 
 */
public class AssetTypeDescriptor implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String COM_OPENMARKET_GATOR_PAGE_PAGE_DEFINITION_MANAGER = "com.openmarket.gator.page.PageDefinitionManager";
	public static final String COM_OPENMARKET_GATOR_PAGE_PAGE_ATTRIBUTE_MANAGER = "com.openmarket.gator.page.PageAttributeManager";
	public static final String COM_OPENMARKET_GATOR_PAGE_PAGE_FILTER_MANAGER = "com.openmarket.gator.page.PageFilterManager";
	public static final String COM_OPENMARKET_GATOR_FLEXFILTERS_FLEX_FILTER_MANAGER = "com.openmarket.gator.flexfilters.FlexFilterManager";
	public static final String COM_OPENMARKET_GATOR_ATTRIBUTES_ATTRIBUTE_MANAGER = "com.openmarket.gator.attributes.AttributeManager";
	public static final String COM_OPENMARKET_GATOR_FLEXTEMPLATES_FLEX_TEMPLATE_MANAGER = "com.openmarket.gator.flextemplates.FlexTemplateManager";
	public static final String COM_OPENMARKET_GATOR_FLEXASSETS_FLEX_ASSET_MANAGER = "com.openmarket.gator.flexassets.FlexAssetManager";
	public static final String COM_OPENMARKET_GATOR_FLEXGROUPTEMPLATES_FLEX_GROUP_TEMPLATE_MANAGER = "com.openmarket.gator.flexgrouptemplates.FlexGroupTemplateManager";
	public static final String COM_OPENMARKET_GATOR_FLEXGROUPS_FLEX_GROUP_MANAGER = "com.openmarket.gator.flexgroups.FlexGroupManager";
	private String assettype;
	private String plural;
	private String logic;
	private String description;
	private boolean canBeChild;
	private String uid;
	private String template;
	private String attr;
	private String filter;
	private String group;
	private String complexAssetClass;

	private FlexItemType whatTypeOfFlexItem;

	public AssetTypeDescriptor(Map<String, String> metaMap) {
		// * TODO - introspection !!!
		assettype = metaMap.get("assettype");
		plural = metaMap.get("plural");
		logic = metaMap.get("logic");

		complexAssetClass = metaMap.get("complexAssetClass");
		description = metaMap.get("description");
		canBeChild = new Boolean(metaMap.get("canbeChild"));
		uid = metaMap.get("uid");
		template = metaMap.get("template");
		attr = metaMap.get("attr");
		filter = metaMap.get("filter");
		group = metaMap.get("group");
		setWhatTypeOfFlexItem();
		
//		if (whatTypeOfFlexItem == null) {
//			System.out.println("Unknown type : " + complexAssetClass);
//		}
	}

	private void setWhatTypeOfFlexItem() {
		if (isFilter()) {
			whatTypeOfFlexItem = FlexItemType.FILTER;
		}

		if (isParent()) {
			whatTypeOfFlexItem = FlexItemType.PARENT;
		}

		if (isParentDef()) {
			whatTypeOfFlexItem = FlexItemType.PARENTDEF;
		}

		if (isAttribute()) {
			whatTypeOfFlexItem = FlexItemType.ATTRIBUTE;
		}

		if (isFlex()) {
			whatTypeOfFlexItem = FlexItemType.FLEXASSET;
		}

		if (isFlexDef()) {
			whatTypeOfFlexItem = FlexItemType.FLEXDEF;
		}

		if (isPageDef()) {
			whatTypeOfFlexItem = FlexItemType.PAGEDEF;
		}

		if (isPageAttr()) {
			whatTypeOfFlexItem = FlexItemType.PAGEATTR;
		}

		if (isPageFilter()) {
			whatTypeOfFlexItem = FlexItemType.PAGEFILTER;
		}
	}

	public FlexItemType getWhatTypeOfFlexItem() {
		return whatTypeOfFlexItem;
	}

	public String getAssettype() {
		return assettype;
	}

	public String getPlural() {
		return plural;
	}

	public String getLogic() {
		return logic;
	}

	public String getDescription() {
		return description;
	}

	public boolean isCanBeChild() {
		return canBeChild;
	}

	public String getUid() {
		return uid;
	}

	public String getTemplate() {
		return template;
	}

	public String getAttr() {
		return attr;
	}

	public String getFilter() {
		return filter;
	}

	public boolean isParent() {
		return FlexItemType.PARENT.getComplexAssetClass().equals(complexAssetClass);
	}

	public boolean isParentDef() {
		return FlexItemType.PARENTDEF.getComplexAssetClass().equals(complexAssetClass);
	}

	public boolean isFlex() {
		return FlexItemType.FLEXASSET.getComplexAssetClass().equals(complexAssetClass);
	}

	public boolean isFlexDef() {
		return FlexItemType.FLEXDEF.getComplexAssetClass().equals(complexAssetClass);
	}

	public boolean isAttribute() {
		return FlexItemType.ATTRIBUTE.getComplexAssetClass().equals(complexAssetClass);
	}

	public boolean isFilter() {
		return FlexItemType.FILTER.getComplexAssetClass().equals(complexAssetClass);
	}

	private boolean isPageFilter() {
		return FlexItemType.PAGEFILTER.getComplexAssetClass().equals(complexAssetClass);
	}

	private boolean isPageAttr() {
		return FlexItemType.PAGEATTR.getComplexAssetClass().equals(complexAssetClass);
	}

	private boolean isPageDef() {
		return FlexItemType.PAGEDEF.getComplexAssetClass().equals(complexAssetClass);
	}

	/*
	 * PD has <complexAssetClass>com.openmarket.gator.flexgrouptemplates.
	 * FlexGroupTemplateManager</complexAssetClass> P has
	 * <complexAssetClass>com.
	 * openmarket.gator.flexgroups.FlexGroupManager</complexAssetClass> has
	 * Template set to PD CD has
	 * <complexAssetClass>com.openmarket.gator.flextemplates
	 * .FlexTemplateManager</complexAssetClass> has Template set to 'PD' C has
	 * <complexAssetClass
	 * >com.openmarket.gator.flexassets.FlexAssetManager</complexAssetClass> has
	 * Template set to CD A has
	 * <complexAssetClass>com.openmarket.gator.attributes
	 * .AttributeManager</complexAssetClass>
	 */
	public String getParentType() {
		return group;
	}

	public String toString() {
		return assettype;
	}
}
